package btree;

import btree.base.TreeNode;

public class Solution_114 {

    public void flatten(TreeNode root) {
        if (root == null)
            return;
        TreeNode dummy = new TreeNode();
        dfs(root, dummy);
        // 把原树结构改成链表
        root.left = null;
        root.right = dummy.right.right; // 跳过 dummy 头
    }

    private TreeNode dfs(TreeNode node, TreeNode tail) {
        if (node == null)
            return tail;

        TreeNode newNode = new TreeNode(node.val);
        tail.right = newNode;
        tail = newNode;

        tail = dfs(node.left, tail);
        tail = dfs(node.right, tail);
        return tail;
    }
}
